function Util(){
    this.ccbReader = null;
    this.bmva = null;
}

var util = new Util();

Util.prototype.newLayerFromCCB = function(ccbFilePath, owner, parentSize, ccbRootPath){
    var reader = new cc.BuilderReader(cc.NodeLoaderLibrary.newDefaultCCNodeLoaderLibrary(), this.bmva);
    reader.setCCBRootPath(dirImg);
    if ((ccbFilePath.length < 5) || (ccbFilePath.toLowerCase().lastIndexOf(".ccbi") != ccbFilePath.length - 5)) 
        ccbFilePath = ccbFilePath + ".ccbi";
    
    this.ccbReader = reader;
    var node = reader.readNodeGraphFromFile(ccbFilePath, owner, parentSize);
    return node;
}

/**
 *
 * @param {Array[key,callback]} arr
 */
Util.prototype.onAssignCCBMemberVariable = function(arr){
    this.bmva = new cc.BuilderMemberVariableAssigner();
    this.bmva.onAssignCCBMemberVariable = function(target, memberVariableName, node){
        util.i18n.members.push(new Array(memberVariableName, node));
        for (i in arr) {
            util.replaceCCBText(memberVariableName, node);
            if (memberVariableName == arr[i].key) {
                arr[i].instance = node;
                node.activate = arr[i].callback;
            }
        }
        return true;
    }
}

/**
 * 洗牌算法
 * @param {Object} length
 */
Util.prototype.getRandomSequence = function(length){
    var seq = new Array();
    var output = new Array();
    for (var i = 0; i < length; i++) {
        seq[i] = i;
    }
    var last_pos = length - 1;
    for (var i = 0; i < length; i++) {
        var num = Math.ceil(Math.random() * last_pos);
        output[i] = seq[num];
        seq[num] = seq[last_pos];
        last_pos = last_pos - 1;
    }
    return output;
}

Util.prototype.isPointInSprite = function(spr, x, y){
    return cc.rectContainsPoint(spr.getBoundingBox(), spr.convertToNodeSpace(cc.p(x, y)));
}

Util.prototype.isSelectSprite = function(spr, x, y){
    var tp = spr.convertToNodeSpace(cc.p(x, y))
    var x = tp.x, y = tp.y
    var size = spr.getContentSize();
    return (x >= 0 && x <= size.width && y >= 0 && y <= size.height);
}

Util.prototype.replaceCCBText = function(member, node){
    if (member == null || node == undefined) {
        return;
    }
    var start = member.indexOf("string_");
    if (typeof(node.setString) == "function") {
        if (start != -1) {
            var text = util.i18n.get(member.substring(start));
            node.setString(text);
            node.enableShadow(cc.size(2, -2), 0.5, 2);
            node.setFontName("Microsoft Yahei");
        }
    }
}

Util.prototype.i18n = {
    members: new Array(),
    callback: function(){
    },
    setLanguage: function(lang){
        var path = dirI18n + this.languageMatch(lang) + ".js";
        
        var d = document;
        var e = d.getElementById("i18n");
        e ? e.parentNode.removeChild(e) : null;
        if (navigator.userAgent.indexOf("Trident/5") > -1) {
            //ie9
            var f = d.createElement('script');
            f.src = path;
            f.id = "i18n";
            f.addEventListener('load', function(){
                this.removeEventListener('load', arguments.callee, false);
                util.i18n.callback();
            }, false);
            d.body.appendChild(f);
        } else {
            var s = d.createElement('script');
            s.async = false;
            s.src = path;
            s.id = "i18n";
            s.addEventListener('load', function(){
                this.removeEventListener('load', arguments.callee, false);
                util.i18n.callback();
            }, false);
            d.body.appendChild(s);
        }
    },
    get: function(obj){
        return Language[obj] ? Language[obj] : "?";
    },
    languageMatch: function(lang){
        var l = lang.toLowerCase();
        if (l.indexOf("en") != -1) {
            l = "en";
        } else if (l.indexOf("ko") != -1) {
            l = "ko";
        } else if (l.indexOf("ar") != -1) {
            l = "ar";
        }
        switch (l) {//zh-Hans, en, ko, ja, zh-Hant
            case "zh":
            case "zh-hans":
            case "zh-cn":
                return "zh-Hans";
                break;
            case "zh-hant":
            case "zh-tw":
                return "zh-Hant";
                break;
            case "en":
                return "en";
                break;
            case "ko":
                return "ko";
                break;
            case "ja":
                return "ja";
                break;
            case "ar":
                return "ar";
                break;
            default:
                return "en";
        }
    }
}

Util.prototype.storage = {
    init: function(){
        try {
            return 'localStorage' in window && window['localStorage'] !== null;
        } catch (e) {
            return false;
        }
    },
    get: function(key){
        if (this.init()) {
            return localStorage.getItem(key);
        }
    },
    set: function(key, value){
        if (this.init()) {
            localStorage.setItem(key, value);
        }
    }
}
var storage = util.storage;
